package com.syezon.wifikey.ad;

import android.app.Activity;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.qq.e.ads.banner2.UnifiedBannerADListener;
import com.qq.e.ads.banner2.UnifiedBannerView;
import com.qq.e.comm.util.AdError;
import com.syezon.wifikey.config.GDTConfig;

public class GDTBannerAd {

    private ViewGroup container;
    private Activity activity;
    private String posId;
    private UnifiedBannerView mBannerView;

    public GDTBannerAd(Activity activity, ViewGroup container, String posId) {
        this.activity = activity;
        this.container = container;
        this.posId = posId;
    }

    public void loadAd() {
        loadGDTBannerAd();
    }

    public void destroy() {
        if (mBannerView != null) {
            mBannerView.destroy();
        }
    }

    private void loadGDTBannerAd() {
        try {
            if (mBannerView != null) {
                container.removeAllViews();
                mBannerView.destroy();
            }
            mBannerView = new UnifiedBannerView(activity, GDTConfig.APP_ID, posId, new UnifiedBannerADListener() {

                boolean reportShow = false;

                @Override
                public void onNoAD(AdError adError) {
                    SDKReportHelper.reportSDKAdv(SDKReportHelper.SOURCE_GDT, posId, SDKReportHelper.OP_TYPE_FAIL);
                }

                @Override
                public void onADReceive() {
                    if (!reportShow) {
                        reportShow = true;
                        SDKReportHelper.reportSDKAdv(SDKReportHelper.SOURCE_GDT, posId, SDKReportHelper.OP_TYPE_SHOW);
                    }
                }

                @Override
                public void onADExposure() {
                }

                @Override
                public void onADClosed() {

                }

                @Override
                public void onADClicked() {
                    SDKReportHelper.reportSDKAdv(SDKReportHelper.SOURCE_GDT, posId, SDKReportHelper.OP_TYPE_CLICK);
                }

                @Override
                public void onADLeftApplication() {

                }

                @Override
                public void onADOpenOverlay() {

                }

                @Override
                public void onADCloseOverlay() {

                }
            });
            // 注意：如果开发者的banner不是始终展示在屏幕中的话，请关闭自动刷新，否则将导致曝光率过低。
            // 并且应该自行处理：当banner广告区域出现在屏幕后，再手动loadAD。
            mBannerView.setRefresh(30);
            container.addView(mBannerView, getUnifiedBannerLayoutParams());
            mBannerView.loadAD();
        } catch (Throwable throwable) {

        }

    }

    /**
     * banner2.0规定banner宽高比应该为6.4:1 , 开发者可自行设置符合规定宽高比的具体宽度和高度值
     * @return
     */
    private FrameLayout.LayoutParams getUnifiedBannerLayoutParams() {
        return new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
    }
}
